package com.example.xianzhi.controller.api;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbAdSmall;
import com.example.xianzhi.entity.TbGoods;
import com.example.xianzhi.entity.TbType;
import com.example.xianzhi.service.GoodsService;
import com.example.xianzhi.service.impl.GoodsServiceImpl;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/apl_Goods")
public class Apl_GoodsController {

    //商品的业务层
    private GoodsService goods_sql = new GoodsServiceImpl();

    /**
     * 添加商品
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/addgoods.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addgoods(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{

        TbGoods goods = new TbGoods();
         goods.setGoods_name("平板");
         goods.setGoods_picture("boy.jsp");
         goods.setGoods_price(299);
         goods.setGoods_old_price(99);
         goods.setGoods_describe("Api15");
         goods.setRelease_id(2);
         goods.setType_id(1);

         //把String转成date
        String time = "2021-12-19";
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sf.parse(time);
        System.out.println("date:"+date);
        goods.setRelease_time(date);

        goods_sql.insertGoods(goods);

    }


    /**
     * 查询商品信息
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectgoods.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoods(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品信息的集合
        Map<String, Object> map = new HashMap<>();
        //查询商品
        TbGoods tbGoods = goods_sql.selectIdGoods(2);

        map.put("code",200);
        map.put("data",tbGoods);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }


    /**
     * 查询商品子图
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectgoodspic.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoodspic(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品子图的集合
        Map<String, Object> map = new HashMap<>();
        //查询商品子图
        List<TbAdSmall> tbAdSmalls = goods_sql.selectGoodsPic(2);


        map.put("code",200);
        map.put("data",tbAdSmalls);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }

    /**
     * 查询发布人的发布了或付款了的订单数据
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectReleaseorder.do")
    @YockMvcAnnotation.ResponseBody
    public String selectReleaseorder(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品订单的集合
        Map<String, Object> map = new HashMap<>();
        //查询订单数据
        List<TbGoods> tbGoods = goods_sql.selectReleaseOrder(1);


        map.put("code",200);
        map.put("data",tbGoods);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }


    /**
     * 查询发布人的订单数据
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectgoodsorder.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoodsorder(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存商品订单的集合
        Map<String, Object> map = new HashMap<>();
        //查询订单数据
        List<TbGoods> tbGoods = goods_sql.selectOrder("1", 1);



        map.put("code",200);
        map.put("data",tbGoods);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }





}
